package net.synck.gcontacts.ui;

import org.eclipse.jface.dialogs.IMessageProvider;
import org.eclipse.jface.dialogs.TitleAreaDialog;
import org.eclipse.jface.preference.PreferenceDialog;
import org.eclipse.swt.SWT;

import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Link;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.dialogs.PreferencesUtil;

public class UserCredentialsDialog extends TitleAreaDialog {

	private Label label = null;
	private Label label1 = null;
	private Text textLogin = null;
	private Text textPassword = null;
	private Button checkBoxSavePassword = null;
	private Label label2 = null;
	private Link link = null;
	
	private String loginString = "";
	private String passwordString = "";
	private boolean saveCredentials = false;
	
	public UserCredentialsDialog(Shell parentShell) {
		super(parentShell);
	}
	
	protected Control createContents(Composite parent) {
		Control contents = super.createContents(parent);
		// Set the title
		setTitle("Google login");
		// Set the message
		setMessage("Please enter your google account data", IMessageProvider.INFORMATION);
		
		textLogin.setText(loginString);
		textPassword.setText(passwordString);		
		checkBoxSavePassword.setSelection(saveCredentials);
		
		return contents;
	}

	
	
	@Override
	public boolean close() {
		loginString = textLogin.getText();
		passwordString = textPassword.getText();
		saveCredentials = checkBoxSavePassword.getSelection();
		return super.close();
	}

	public String getLoginString() {
		return loginString;
	}

	public void setLoginString(String loginString) {
		this.loginString = loginString;
	}

	public String getPasswordString() {
		return passwordString; 
	}

	public void setPasswordString(String passwordString) {
		this.passwordString = passwordString;
	}

	public boolean isSaveCredentials() {
		return saveCredentials; 
	}

	public void setSaveCredentials(boolean saveCredentials) {
		this.saveCredentials = saveCredentials;
	}

	@Override
	protected Control createDialogArea(Composite parent) {
		GridData gridData3 = new GridData();
		gridData3.horizontalAlignment = GridData.BEGINNING;
		gridData3.verticalAlignment = GridData.CENTER;
		GridData gridData2 = new GridData();
		gridData2.horizontalAlignment = GridData.END;
		gridData2.verticalAlignment = GridData.CENTER;
		GridData gridData1 = new GridData();
		gridData1.horizontalAlignment = GridData.FILL;
		gridData1.grabExcessHorizontalSpace = true;
		gridData1.verticalAlignment = GridData.CENTER;
		GridData gridData = new GridData();
		gridData.horizontalAlignment = GridData.FILL;
		gridData.grabExcessHorizontalSpace = true;
		gridData.verticalAlignment = GridData.CENTER;
		GridLayout gridLayout = new GridLayout();
		gridLayout.numColumns = 2;
		label = new Label(parent, SWT.NONE);
		label.setText("Login:");
		textLogin = new Text(parent, SWT.BORDER);
		textLogin.setLayoutData(gridData);
		label1 = new Label(parent, SWT.NONE);
		label1.setText("Password:");
		textPassword = new Text(parent, SWT.BORDER | SWT.PASSWORD);
		textPassword.setLayoutData(gridData1);
		label2 = new Label(parent, SWT.NONE);
		label2.setText("");
		checkBoxSavePassword = new Button(parent, SWT.CHECK);
		checkBoxSavePassword.setText("Save password");
		checkBoxSavePassword.setLayoutData(gridData2);
		Label filler = new Label(parent, SWT.NONE);
		link = new Link(parent, SWT.NONE);
		link.setText("<a>Internet proxy settings...</a>");
		link.setLayoutData(gridData3);
		parent.setLayout(gridLayout);
		
		link.addSelectionListener(new SelectionListener() {

			@Override
			public void widgetDefaultSelected(SelectionEvent e) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void widgetSelected(SelectionEvent e) {
				openProxyPreferences();				
			}
			
		});
		
		return parent;
	}

	private void openProxyPreferences() {
		//id = org.eclipse.ui.net.NetPreferences
		//id = org.eclipse.ui.net.proxy

		PreferenceDialog pref = PreferencesUtil.createPreferenceDialogOn(
				this.getShell(), "org.eclipse.ui.net.NetPreferences", null, null);
		if (pref != null)
			pref.open();
	}
	
	
}
